//项目二交通信号灯
int carRed = 12;
int carYellow = 11;
int carGreen = 10;
int button = 9;
int pedRed = 8;
int pedGreen = 7;
int crossTime = 5000;
unsigned long changeTime;
void setup() {
  // put your setup code here, to run once:
  pinMode(carRed,OUTPUT);
  pinMode(carYellow,OUTPUT);
  pinMode(carGreen,OUTPUT);
  pinMode(pedRed,OUTPUT);
  pinMode(pedGreen,OUTPUT);
  pinMode(button,INPUT);//按钮设置为输入模式

  digitalWrite(carGreen,HIGH);//车行绿灯亮
  digitalWrite(pedRed,HIGH);//人行红灯亮
}

void loop() {
  // put your main code here, to run repeatedly:
  int state = digitalRead(button);
  //检测按钮状态及时间间隔
  if(state==HIGH&&(millis()-changeTime)>5000){
    //调用变灯函数
    changeLights();
  }
}
void changeLights(){
  //汽车绿灯变黄灯
  digitalWrite(carGreen,LOW);
  digitalWrite(carYellow,HIGH);
  delay(2000);//等待2秒

  //汽车黄灯变红灯
  digitalWrite(carYellow,LOW);
  digitalWrite(carRed,HIGH);
  delay(1000);//为安全考虑等待1秒

  //行人红灯变绿灯
  digitalWrite(pedRed,LOW);
  digitalWrite(pedGreen,HIGH);

  //行人绿灯亮，持续crossTime毫秒
  delay(crossTime);

  //闪烁行人绿灯，提示时间快到
  for(int x=0;x<10;x++){
    digitalWrite(pedRed,LOW);
    digitalWrite(pedGreen,HIGH);
    delay(250);
    digitalWrite(pedGreen,LOW);
    digitalWrite(pedRed,HIGH);
    delay(250);
  }

  //恢复汽车绿灯亮状态
  digitalWrite(carRed,LOW);
  digitalWrite(carYellow,HIGH);
  delay(1000);
  digitalWrite(carYellow,LOW);
  digitalWrite(carGreen,HIGH);

  //更新changeTime
  changeTime = millis();
}
